<template>
  <div id='home'>
    <!-- 头部导航 -->
    <NavBar class="home-nav">
      <div slot="center">购物街</div>
    </NavBar>

    <TabControl v-show='isTabFixed' :titles="['流行','新款','精选']" @tabClick='tabClick' ref='tabControl_1'
      class="tab-control"></TabControl>

    <Scroll class="content" ref='scroll' :probeType='3' @scroll='contentScroll' :pullUpLoad='true'
      @pullingUp='loadMore'>
      <!-- 轮播图 -->
      <HomeSwiper :banners='banners' @swiperImageLoad='swiperImageLoad'></HomeSwiper>
      <!-- 推荐模块 -->
      <RecommendView :recommends='recommends'></RecommendView>
      <!-- 展示区 -->
      <FeatureView></FeatureView>
      <!-- 首页选项卡 -->
      <TabControl :titles="['流行','新款','精选']" @tabClick='tabClick' ref='tabControl'>
      </TabControl>
      <!-- 商品列表展示区 -->
      <GoodsList :goods='showGoods'></GoodsList>
    </Scroll>

    <!-- 回到顶部 -->
    <BackTop @click.native='backTop' v-show='isShowBackTop'></BackTop>


  </div>
</template>

<script>
  import NavBar from 'components/common/navbar/NavBar'
  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from 'components/content/goods/GoodsList'
  import Scroll from 'components/common/scroll/Scroll'

  import HomeSwiper from './childComps/HomeSwiper'
  import RecommendView from './childComps/RecommendView'
  import FeatureView from './childComps/FeatureView'

  import {
    getHomeMultidata,
    getHomeGoods
  } from 'network/home'

  import { debounce } from '@/common/utils.js'
  import { itemListenerMixin, backTopMixin } from '@/common/mixin.js'

  export default {
    name: '',
    mixins: [itemListenerMixin, backTopMixin],
    data() {
      return {
        banners: [],
        recommends: [],
        goods: {
          'pop': { page: 0, list: [] },
          'new': { page: 0, list: [] },
          'sell': { page: 0, list: [] },
        },
        currentType: 'pop',
        tabOffsetTop: 0,
        isTabFixed: false,
        saveY: 0,
      }
    },

    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },

    destroyed() {
      console.log('Home Destroyed');
    },

    activated() {
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      // 1 保存离开的时候scroll组件滚动位置的Y值
      this.saveY = this.$refs.scroll.getScrollY()
      // 2 取消全局事件的监听
      this.$bus.$off('itemImageLoad', this.itemImgListener)
    },

    created() {
      //请求更多数据
      this.getHomeMultidata()
      //请求商品列表数据
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')

    },

    mounted() {
    },

    methods: {
      /**
       *   getHomeMultidata()
       *   首页发送网络请求,获取页面商品基本数据 
       */
      getHomeMultidata() {
        getHomeMultidata().then(res => {
          this.banners = res.data.banner.list
          this.recommends = res.data.recommend.list
        })
      },
      /**
       *   getHomeGoods(type,page)
       *   发送网络请求,获取商品列表展示的数据
       *   参数1:根据选项卡选择数据的类型
       *   参数2:需要获取的数据的页码(后端设计了分页查询)
       */
      getHomeGoods(type) {
        let page = this.goods[type].page + 1
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page += 1

          //完成上拉加载更多
          this.$refs.scroll.finishPullUp()
        })
      },
      /**
       *  tabClick()
       *  tabControl点击,回调函数 
       */
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop'
            break;
          case 1:
            this.currentType = 'new'
            break;
          case 2:
            this.currentType = 'sell'
            break;
        }
        // 让上下两个tabControl的点击状态保持一致
        this.$refs.tabControl_1.currentIndex = index
        this.$refs.tabControl.currentIndex = index
      },
      /**
       *  contentScroll()
       *  滚动条滚动位置回调函数 
       */
      contentScroll(position) {
        // 1 回到顶部组件是否显示
        this.isShowBackTop = -position.y > 1000
        // 2 决定tabControl是否吸顶
        this.isTabFixed = -position.y > this.tabOffsetTop
      },
      /**
       *  loadMore()scroll组件触发上拉加载更多
       *  Home组件收到消息后,开始执行上拉加载更多 
       */
      loadMore() {
        this.getHomeGoods(this.currentType)
      },
      /**
       *  swiperImageLoad()
       *  轮播图图片加载完成之后触发的回调函数 
       *  轮播图里面发送端设置了 标志位实现节流阀 事件总共触发一次 其实就可以获取到tabControl.offsetTop
       */
      swiperImageLoad() {
        //获取tabControl的offsetTop
        // this.tabOffsetTop = this.$refs.tabControl.$el
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
      }

    },
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll,

    }
  }
</script>

<style scoped>
  #home {
    /* padding-top: 44px; */
    height: 100vh;
    position: relative;
  }

  .home-nav {
    background-color: deeppink;
    color: #fff;
    font-size: 18px;

    /* position: fixed;
    left: 0;
    right: 0;
    top: 0;
    z-index: 9; */
  }

  /* .tab-control {
    position: sticky;
    top: 44px;
    z-index: 9;
  } */

  .content {
    /* height: 300px; */
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  /* .content {
    height: calc(100% - 93px);
    overflow: hidden;
    margin-top: 44px;
  } */
  .tab-control {
    position: relative;
    z-index: 9;
  }
</style>